// 模拟数据：此函数用于生成一个登录的用户信息
function createUserList() {
    return [
        {
            userId: 1,
            username: 'admin',
            phone: '13800000000',
            token: 'admin-token',
            avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
            gender: '男',
        },
        {
            userId: 2,
            username: 'user1',
            phone: '18975806269',
            token: 'user1-token',
            avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
            gender: '女',
        }
    ];
}

// 模拟验证码生成
function generateCode() {
    return Math.floor(100000 + Math.random() * 900000).toString(); // 生成六位数验证码
}



// 模拟生成登录日志
function createLoginLogs(pageSize, pageNum) {
    const totalLogs = 5; // 假设总共有5条日志
    const logs = [
        { id: 5, username: "xx", systemType: "Windows 10", browserType: "Chrome", loginTime: "2025-02-07T18:50:50", userId: 111 },
        { id: 4, username: "xx", systemType: "Windows 10", browserType: "Chrome", loginTime: "2025-02-07T18:49:07", userId: 111 },
        { id: 3, username: "xx", systemType: "Windows 10", browserType: "Chrome", loginTime: "2025-02-07T18:48:43", userId: 111 },
        { id: 2, username: "xx", systemType: "Windows 10", browserType: "Chrome", loginTime: "2025-02-07T18:48:30", userId: 111 },
        { id: 1, username: "xx", systemType: "Windows 10", browserType: "Chrome", loginTime: "2025-02-07T18:47:22", userId: 111 },
    ];

    // 分页处理
    const start = (pageNum - 1) * pageSize;
    const end = start + pageSize;

    return {
        pageSize: pageSize,
        total: totalLogs,
        pages: Math.ceil(totalLogs / pageSize),
        datas: logs.slice(start, end),
        pageNum: pageNum
    };
}


export default [
    // 发送验证码接口
    {
        url: '/api/users/sendMsg',
        method: 'post',
        response: ({ body }) => {
            const { phone } = body;
            if (!phone) {
                return {
                    code: 400,
                    msg: '手机号不能为空',
                };
            }

            // 模拟生成验证码
            const code = generateCode();
            console.log(`发送验证码：${code} 到手机号：${phone}`);

            // 返回成功信息
            return {
                code: 200,
                msg: '短信发送成功',
                data: code, // 返回验证码
            };
        },
    },

    // 登录接口
    {
        url: '/api/users/login',
        method: 'post',
        response: ({ body }) => {
            const { phone, code } = body;

            // 校验手机号和验证码
            if (!phone || !code) {
                return {
                    code: 400,
                    msg: '手机号和验证码不能为空',
                };
            }

            // 假设验证手机号和验证码正确
            const user = createUserList().find(user => user.phone === phone);

            if (!user) {
                return {
                    code: 201,
                    msg: '手机号未注册',
                };
            }

            // 返回登录成功的用户信息，符合 loginResponseData 类型
            return {
                code: 200,
                msg: '登录成功',
                data: {
                    username: user.username,
                    token: user.token,
                    userId: user.userId,
                    phone: user.phone,
                },
            };
        },
    },

    // 获取用户信息接口
    {
        url: '/api/user/info',
        method: 'get',
        response: (request) => {
            const token = request.headers.token;

            // 根据 token 查找用户信息
            const user = createUserList().find(item => item.token === token);

            if (!user) {
                return {
                    code: 201,
                    msg: '用户未登录或 token 无效',
                };
            }

            // 返回用户信息，符合 userResponseData 类型
            return {
                code: 200,
                msg: '获取用户信息成功',
                data: {
                    id: user.userId,
                    username: user.username,
                    avatar: user.avatar,
                    gender: user.gender,
                    phone:user.phone
                },
            };
        },
    },
   // 修改用户名接口
{
    url: '/api/users/change',
    method: 'put',
    response: (request ) => {
        // 从请求头中获取 token 和 username
        const token = request.headers.token;
        const { username } = request.body;

        if (!username || !token) {
            return {
                code: 400,
                msg: '用户名和 token 不能为空',
            };
        }

        // 根据 token 查找用户信息
        const user = createUserList().find(item => item.token === token);

        if (!user) {
            return {
                code: 201,
                msg: '用户未登录或 token 无效',
            };
        }

        // 更新用户的用户名
        user.username = username;

        // 返回成功信息，并返回更新后的用户数据
        return {
            code: 200,
            msg: '用户名修改成功',
            data: {
                userId: user.userId,
                username: user.username,
                phone: user.phone,
                avatar: user.avatar,
                gender: user.gender,
                token: user.token,
            },
        };
    },
},

    // 获取登录日志接口
    {
        url: '/api/logs/loginlogs',
        method: 'get',
        response: ({ query, headers }) => {
            const { pageSize, pageNum } = query;
            const token = headers.token;

            if (!token) {
                return {
                    code: 401,
                    msg: '未授权，缺少 token'
                };
            }

            // 获取登录日志数据
            const data = createLoginLogs(parseInt(pageSize), parseInt(pageNum));

            return {
                code: 200,
                msg: '成功',
                data: data,
            };
        }
    },


];
